package name.koppernickus.lsp.analyzer;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;

import java.lang.reflect.Method;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Arrays.asList;

public class OverridableMethodSignature {

    public static OverridableMethodSignature method(String name, Class<?>... paramaters) {
        return new OverridableMethodSignature(name, paramaters);
    }

    private final String name;
    private final List<Class<?>> paramaters;

    public OverridableMethodSignature(Method method) {
        this(method.getName(), method.getParameterTypes());
    }

    public OverridableMethodSignature(String name, Class<?>[] paramaters) {
        this(name, asList(paramaters));
    }

    public OverridableMethodSignature(String name, List<Class<?>> paramaters) {
        checkNotNull(name);
        checkNotNull(paramaters);
        this.name = name;
        this.paramaters = ImmutableList.copyOf(paramaters);
    }

    public String getName() {
        return name;
    }

    public List<Class<?>> getParamaters() {
        return paramaters;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        OverridableMethodSignature that = (OverridableMethodSignature) o;
        if (!name.equals(that.name)) return false;
        if (!paramaters.equals(that.paramaters)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + paramaters.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return name.toString() + "(" + Joiner.on(", ").join(paramaters) + ")";
    }
}
